import { Controller, Get, Req, Res } from "@nestjs/common";
import type { Request, Response } from 'express'
import { Public } from "src/decorators/is-public.decorator";

@Controller('cookie')
export class CookieController {
    @Public()
    @Get('set')
    setCookie(@Res() res: Response) {
        res.cookie('username', 'leslie', { 
            httpOnly: true,
            maxAge: 60 * 1000 * 60,
            secure: true,
            sameSite: 'lax',
         });
        res.send('Cookie set');
    }

    @Public()
    @Get('get')
    getCookie(@Req() req: Request) {
        const username = req.cookies.username || 'no username';
        return `Current cookie user name is ${username}`;
    }

    @Public()
    @Get('clear')
    clearCookie(@Res() res: Response) {
        res.clearCookie('username');
        res.send('Cookie cleared');
    }
}